
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { scrollMixin } from "utils/mixin";
import courseItem from "../../../components/new_diy/courseItem/courseItem.vue"
export default {
  name:'',
  //import引入的组件需要注入到对象中才能使用
  components: {
    courseItem
  },
  mixins: [scrollMixin], //加载更多
  data() {
  return {
    courseList:[], //章节列表
    iPnoneBottomBol: false, //是否是苹果手机
    keyword:'', //关键词
    page:1,
    lecturerId:'', //讲师ID
    isLoadMore: true,
    total_page: 0,
    timer:''
  };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {
    
  },
  //方法集合
  methods: {
    courseSearch(value) {
      this.keyword = value;
      this.page = 1;
      this.courseList = [];
      // 防抖
      if (this.timer) clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        this.debounce(this.getCourseList());
      }, 500);
    },
    getiPnoneBottom() { //检测是否为苹果手机
      var userAgent = navigator.userAgent;
      var isiOS = !!userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
      isiOS?this.iPnoneBottomBol = true:this.iPnoneBottomBol = false;
    },
    debounce(fn, delay) {
      let last, deferTimer;
      return function(args) {
        let that = this;
        let _args = arguments;
        let now = +new Date();
        if (last && now < last + delay) {
          clearTimeout(deferTimer);
          deferTimer = setTimeout(function() {
            last = now;
            fn.apply(that, _args);
          }, delay);
        } else {
          last = now;
          fn.apply(that, _args);
        }
      };
    },
    getCourseList() {
      let params = {
        keyword:this.keyword,
        page: this.page,
        lecturerId: this.lecturerId,
        basic_info:1
      }
      $http.post("plugin.course-supply.api.course.index", params, "加载中..").then(response => {
        if (response.result == 1) {
          let res = response.data;
          let basicInfo = response.basic_info;
          this.isLoadMore = true;
          this.total_page = res.last_page;
          if (!this.total_page) {
            this.page = 0;
          }
          this.courseList = res.data;
          this.$nextTick(() => {
            this.fun.wxShare(
              "",
              { mid: this.fun.getKeyByMid() },
              {
                link: window.location.href,
                title: '课程供应链-'+basicInfo?.home.mailInfo?.name,
                imgUrl: basicInfo?.home.mailInfo?.logo_url,
                description: '课程供应链'
              }
            );
          },50);
        } else {
          this.page = this.page - 1;
          this.isLoadMore = false;
          this.$dialog.alert({
            message: response.msg
          });
        }
      });
    },
    //获取更多数据
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page = this.page + 1;
        let params = {
          keyword:this.keyword,
          page: this.page,
          lecturerId: this.lecturerId,
          basic_info:1
        }
        $http.post("plugin.course-supply.api.course.index", params, "加载中").then(
          res => {
            this.isLoadMore = true;
            if (res.result === 1) {
              let myData = res.data.data;
              this.courseList = [...this.courseList, ...myData]; //数组拼接
            } else {
              this.page = this.page - 1;
              this.isLoadMore = false;
            }
          },
          function(response) {
            console.log("response", response);
            // error callback
          }
        );
      }
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {

  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {

  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {
    this.page = 1;
    this.getCourseList();
    this.getiPnoneBottom();
  }, //如果页面有keep-alive缓存功能，这个函数会触发
}